﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DoAn_BumTruong_DesignPattern
{
    class MenuController : Abstract
    {
        public List<Menu> SelectAll()
        {
            using (var db = new DBContextEntity())
            {
                var query = from mn in db.Menues
                            select mn;
                return query.ToList();
            }
        }
        //Select By MenuCha
        public List<Menu> SelectParentID()
        {
            using (var db = new DBContextEntity())
            {
                var query = from mn in db.Menues
                            where mn.ParentID != null
                            select mn;
                return query.ToList();
            }
        }
        //Select By MenuCon
        public List<Menu> SelectParentIDChidren()
        {
            using (var db = new DBContextEntity())
            {
                var query = from mn in db.Menues
                            where mn.ParentID == null
                            select mn;
                return query.ToList();
            }
        }
        //Get by MenuID
        public Menu Single(string menuID)
        {
            int mnID = Convert.ToInt32(menuID);
            using (var db = new DBContextEntity())
            {
                return db.Menues.SingleOrDefault(mn => mn.MenuID.Equals(mnID));
            }
        }
        //paging
        public List<Menu> Paging(string page, out int howManyPage)
        {
            int pageSize = GlobalConfiguration.PageSize;
            howManyPage = (int)Math.Ceiling((double)SelectAll().Count / (double)pageSize);
            return SelectAll().Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        //Select NameByParent
        //public string ParentName
        //{
        //    //get {
        //    //    return Single(Convert.ToString(mn=>mn.))
        //    //}

        //}
        //Get By MenuType=MenuRightTop
        public List<Menu> GetMenuRightTop(string menutype)
        {
            int mntype = Convert.ToInt32(menutype);
            using (var db = new DBContextEntity())
            {
                var query = from mn in db.Menues
                            where mn.MenuType.Equals(mntype)
                            select mn;
                return query.ToList();
            }
        }
        public override bool Add(object o)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(object o)
        {
            throw new NotImplementedException();
        }

        public override bool Update(object o)
        {
            throw new NotImplementedException();
        }

        //Update Active
        public bool UpdateActive(int id, bool state)
        {
            using (var db = new DBContextEntity())
            {
                var menu = db.Menues.Find(id);
                if (menu != null)
                {
                    menu.MenuID = id;
                    menu.Active = state;
                    db.SaveChanges();
                }
                return true;
            }
        }

        //Update OrderNo
        public bool UpdateOrderNo(int id, int order)
        {
            using (var db = new DBContextEntity())
            {
                var menu = db.Menues.Find(id);
                if (menu != null)
                {
                    menu.MenuID = id;
                    menu.OrderNo = order;
                    db.SaveChanges();
                }
                return true;
            }
        }

        //Update MenuType
        public bool UpdateMenuType(int id, int type)
        {
            using (var db = new DBContextEntity())
            {
                var menu = db.Menues.Find(id);
                if (menu != null)
                {
                    menu.MenuID = id;
                    menu.MenuType = type;
                    db.SaveChanges();
                }
                return true;
            }
        }
    }
}